var MaterialLossForm = function () {
  var MaterialLossModel = function (data) {
    BaseModel.call(this);

    var requiredFields = ['orderTotalStart', 'orderTotalEnd', 'wasteRate', 'wasteTotal', 'status'];
    var self = this;
    var fn = {
      initData: function (data) {
        if (data === undefined) {
          data = {
            status: 1
          };
        } else {
          self.id = data.id;
        }

        var fields = requiredFields;
        for (var i = 0; i < fields.length; i++) {
          var field = fields[i];
          var value = data[field];
          if (SysUtil.isEmpty(value)) {
            value = undefined;
          }
          self[field] = value;
        }
      }
    };

    this.checkValid = function () {
      var fields = ['orderTotalStart', 'orderTotalEnd', 'status'];
      for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        var value = this[field];
        if (SysUtil.isEmpty(value)) {
          return field;
        }
      }

      if (this.wasteRate === undefined && this.wasteTotal === undefined) {
        return 'waste';
      }
      return '';
    };

    this.getObject = function () {
      var fields = ['orderTotalStart', 'orderTotalEnd', 'status'];
      var postData = {};
      this.setPostData(postData, fields, false);

      var wasteRate = this.wasteRate;
      if (wasteRate === undefined) {
        wasteRate = 0;
      } else {
        wasteRate = _.round(wasteRate / 100, 3);
      }
      postData.wasteRate = wasteRate;

      var wasteTotal = this.wasteTotal;
      if (wasteTotal === undefined) {
        wasteTotal = 0;
      }
      postData.wasteTotal = wasteTotal;

      var id = this.id;
      if (!SysUtil.isEmpty(id)) {
        postData.id = id;
      }

      return postData;
    };

    fn.initData(data);
  };

  var template = /* html */ `
    <div class="material-loss-form" v-loading.fullscreen.lock="loading">
      <el-form label-width="auto">
        <el-form-item label="订单数">
          <template v-if="type==='add'">
            <el-input-number v-model="lossForm.orderTotalStart" :controls="false" :min="1" :max="lossForm.orderTotalEnd"
              :precision="0" placeholder="请输入订单数最小值" style="width:160px">
            </el-input-number>
            <span>~</span>
            <el-input-number v-model="lossForm.orderTotalEnd" :controls="false" :min="lossForm.orderTotalStart?lossForm.orderTotalStart:1"
              :precision="0" placeholder="请输入订单数最大值" style="width:160px">
            </el-input-number>
          </template>
          <span v-else>{{lossForm.orderTotalStart}} ~ {{lossForm.orderTotalEnd}}</span>
        </el-form-item>
        <el-form-item label="损耗率">
          <el-input-number is-decimal-zero v-model="lossForm.wasteRate" :controls="false" :min="0" :precision="1" placeholder="请输入损耗率百分比"
            style="width:300px">
          </el-input-number>
          <span>%</span>
        </el-form-item>
        <el-form-item label="损耗数量">
          <el-input-number is-decimal-zero v-model="lossForm.wasteTotal" :controls="false" :min="0" :precision="3" placeholder="请输入损耗数量"
            style="width:300px">
          </el-input-number>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="lossForm.status" style="width:300px" placeholder="请选择状态">
            <el-option v-for="item in statusList" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div style="text-align:center">
        <el-button type="primary" @click="ensure">
          <span v-if="type==='add'">新增</span>
          <span v-else>修改</span>
        </el-button>
      </div>
    </div>
  `;

  return {
    props: {
      loss: {
        type: Object,
        default: function () {
          return null;
        }
      },
      type: {
        type: String,
        default: 'add'
      }
    },

    data: function () {
      var lossForm;
      if (this.type === 'add') {
        lossForm = new MaterialLossModel();
      } else {
        lossForm = new MaterialLossModel(this.loss);
      }
      return {
        loading: false,
        lossForm: lossForm,
        statusList: i18n.getDropDown('bomBusiness.status', [1, 0])
      };
    },

    methods: {
      ensure: function () {
        var lossForm = this.lossForm;
        var code = lossForm.checkValid();
        if (code !== '') {
          var msg = i18n.getMsg(`bomBusiness.materialLossSetting.error.${code}`);
          this.$alert(msg, '信息');
          return;
        }

        if (this.type === 'add') {
          this.toAddLoss(lossForm);
        } else {
          this.toUpdateLoss(lossForm);
        }
      },

      toAddLoss: function (lossForm) {
        var postData = lossForm.getObject();
        this.loading = true;
        var self = this;
        ajaxRequest('createOrderForBomOrder/saveWasteRate', postData).then(function () {
          self.loading = false;
          self.$alert('新增损耗配置成功', '信息', {
            callback: function () {
              self.$emit('save-success');
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      },

      toUpdateLoss: function (lossForm) {
        var postData = lossForm.getObject();
        this.loading = true;
        var self = this;
        ajaxRequest('createOrderForBomOrder/updateWasteRate', postData).then(function () {
          self.loading = false;
          self.$alert('修改损耗配置成功', '信息', {
            callback: function () {
              self.$emit('save-success');
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    template: template
  };
}();
